<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/admin/css/other/department.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">权限名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="请输入权限名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">权限编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="percode" placeholder="请输入权限编码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态:</label>
                    <div class="layui-input-inline">
                        <select id="available" name="available">
                            <option value="">可用状态</option>
                            <option value="1">正常</option>
                            <option value="2">停用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="permission-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-row layui-col-space15">
    <div class="layui-col-md3">
        <div class="layui-card">
            <div class="layui-card-body">
                <div id="organizationTreeContent" style="overflow: auto">
                    <ul id="organizationTree" class="dtree organizationTree" data-id="9527"></ul>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-md9">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="organization-table" lay-filter="organization-table"></table>
            </div>
        </div>
    </div>
</div>


<!-- 添加修改的弹出层 -->
<div style="display: none;padding: 3px" id="saveOrUpdateDiv">
    <form id="dataFrm" lay-filter="dataFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">父级菜单:</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <ul id="menuTree" class="dtree" data-id="0"></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限名称:</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入权限名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限编码:</label>
            <div class="layui-input-block">
                <input type="text" name="percode" autocomplete="off" placeholder="请输入权限编码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否可用:</label>
                <div class="layui-input-block">
                    <input type="radio" name="available" value="1" title="可用" checked="">
                    <input type="radio" name="available" value="2" title="不可用">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">排序码:</label>
                <div class="layui-input-block">
                    <input type="text" name="ordernum" id="ordernum" lay-verify="required|number" autocomplete="off"
                           placeholder="请输入排序码" class="layui-input">
                </div>
            </div>
        </div>

        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="permission-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<script type="text/html" id="organization-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" shiro:hasPermission="Permission:create">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="organization-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" shiro:hasPermission="Permission:update"><i
            class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" shiro:hasPermission="Permission:delete"><i
            class="layui-icon layui-icon-delete"></i></button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'dtree'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;


        let cols = [
            [
                {
                    title: 'ID',
                    field: 'id',
                    hide: false,
                    align: 'center'
                },
                {
                    title: '父级ID',
                    field: 'pid',
                    hide: true,
                    align: 'center'
                },
                {
                    title: '权限名称',
                    field: 'title',
                    align: 'center'
                },
                {
                    title: '权限编码',
                    field: 'percode',
                    align: 'center'
                },
                {
                    title: '是否可用',
                    field: 'available',
                    align: 'center',
                    templet: function (d) {
                        return d.available == 1 ? '<font color=blue>可用</font>' : '<font color=red>停用</font>';
                    }
                },
                {
                    title: '排序码',
                    field: 'ordernum',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#organization-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        let DTree = dtree.render({
            elem: "#organizationTree",
            //data: data,
            dataStyle: "layuiStyle",
            initLevel: "2", //默认展开层级为1
            line: true, // 有线树
            ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
            icon: ["0", "2"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
            method: 'get',
            url: "/menu/loadTreeMenu",
        });

        let tableIns = table.render({
            elem: '#organization-table',
            url: '/permission/loadAllPermissionByPage',
            height: 'full-150',
            page: true,
            cols: cols,
            toolbar: '#organization-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print'],
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });

        // 绑定节点点击事件
        dtree.on("node(organizationTree)", function (obj) {
            tableIns.reload({
                url: '/permission/loadAllPermissionByPage?id=' + obj.param.nodeId
            })
        });

        //渲染添加和修改页面的下拉树
        var addOrUpdateMenuTree = dtree.renderSelect({
            elem: "#menuTree",
            url: "/menu/loadTreeMenu",
            dataStyle: "layuiStyle",
            initLevel: "2", //默认展开层级为1
            line: true, // 有线树
            ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
            icon: ["0", "2"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
            method: 'get',
            // dataFormat: "list",  //配置data的风格为list
            selectTips: "请选父级部门",
            selectInputName: {
                nodeId: "pid"//,  //提交到后台的部门ID的key
            }
        });


        //表头监听
        table.on('tool(organization-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        //行监听
        table.on('toolbar(organization-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //表格监听
        form.on('submit(permission-query)', function (data) {
            table.reload('organization-table', {
                where: data.field
            })
            return false;
        });

        let mainIndex;
        let url;
        window.add = function () {
            mainIndex = layer.open({
                type: 1,
                title: '新增权限',
                shade: 0.1,
                area: ['700px', '450px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/permission/addPermission";
                    $("#dataFrm")[0].reset();
                    //重新加载树
                    addOrUpdateMenuTree.refreshTree();

                    $.post("/menu/loadMaxOrderNum", function (obj) {
                        $("#ordernum").val(obj.data);
                    });

                    $(".dtree-select").removeClass("dtree-select-show");//解决弹出层里面的下列树展开的问题
                }
            });
        }

        window.edit = function (obj) {
            mainIndex = layer.open({
                type: 1,
                title: '修改权限',
                shade: 0.1,
                area: ['700px', '450px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/permission/updatePermission";
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm", obj.data);

                    //树形下拉框的回显
                    dtree.dataInit("menuTree", obj.data.pid + "");
                    dtree.selectVal("menuTree");

                    $(".dtree-select").removeClass("dtree-select-show");//解决弹出层里面的下列树展开的问题
                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该权限吗？', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/permission/deletePermission/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            DTree.reload();//重新加载左边菜单树
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }

                })
            });

        }

        //监听按钮提交事件
        form.on("submit(permission-save)", function (data) {
            let params = $("#dataFrm").serialize();
            $.post(url, params, function (obj) {
                if (obj.code == 200) {
                    tableIns.reload();
                    //刷新左边的树
                    DTree.reload();
                }
                layer.msg(obj.msg);
                layer.close(mainIndex);
            })
            return false;//阻止同步提交
        });

        window.refresh = function (param) {
            table.reload('organization-table');
        }
    })
</script>
</body>
</html>
